package com.meilingcloud.activiti.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.meilingcloud.activiti.domain.ActWfStock;
import com.meilingcloud.activiti.domain.dto.ActWfStockSaveDto;
import com.meilingcloud.activiti.service.IActWfStockService;
import com.meilingcloud.common.annotation.Log;
import com.meilingcloud.common.core.controller.BaseController;
import com.meilingcloud.common.core.domain.AjaxResult;
import com.meilingcloud.common.core.page.TableDataInfo;
import com.meilingcloud.common.enums.BusinessType;
import com.meilingcloud.common.utils.SecurityUtils;
import com.meilingcloud.common.utils.poi.ExcelUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 出入库单据 Controller
 *
 * @author chenl
 * @date 2021-03-15
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/activiti/stock")
public class ActWfStockController extends BaseController {

    private final IActWfStockService iActWfStockService;

    /**
     * 查询当前用户出入库单据 列表
     */
    @PreAuthorize("@ss.hasPermi('activiti:stock:list')")
    @GetMapping("/list")
    public TableDataInfo list(ActWfStock actWfStock) {
        actWfStock.setCreateBy(SecurityUtils.getUsername());
        startPage();
        List<ActWfStock> list = iActWfStockService.queryStockWithTaskList(actWfStock);
        return getDataTable(list);
    }

    /**
     * 查询当前部门下所有出入库单据
     */
    @PreAuthorize("@ss.hasPermi('activiti:stock:list')")
    @GetMapping("/listAll")
    public TableDataInfo listAll(ActWfStock actWfStock) {
        List<String> userNames = iActWfStockService.queryUserByDept();
        startPage();
        List<ActWfStock> list = iActWfStockService.queryStockInDeptList(actWfStock, userNames);
        return getDataTable(list);
    }

    /**
     * 导出出入库单据 列表
     */
    @PreAuthorize("@ss.hasPermi('activiti:stock:export')")
    @Log(title = "样本单据 ", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(ActWfStock actWfStock) {
        List<ActWfStock> list = iActWfStockService.queryList(actWfStock);
        ExcelUtil<ActWfStock> util = new ExcelUtil<ActWfStock>(ActWfStock.class);
        return util.exportExcel(list, "stock");
    }

    /**
     * 获取出入库单据 详细信息
     */
    @PreAuthorize("@ss.hasPermi('activiti:stock:query')")
    @GetMapping(value = "/{stockId}")
    public AjaxResult getInfo(@PathVariable("stockId") String stockId) {
        return AjaxResult.success(iActWfStockService.getStockDetail(stockId));
    }

    /**
     * 根据实例id获取单据详细信息
     */
    @PreAuthorize("@ss.hasPermi('activiti:stock:query')")
    @GetMapping(value = "/byInstanceId/{instanceId}")
    public AjaxResult getInfoByInstanceId(@PathVariable("instanceId") String instanceId) {
        return AjaxResult.success(iActWfStockService.getOne(new LambdaQueryWrapper<ActWfStock>().eq(ActWfStock::getInstanceId, instanceId)));
    }

    /**
     * 新增出入库单据
     */
    @PreAuthorize("@ss.hasPermi('activiti:stock:add')")
    @Log(title = "样本单据 ", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ActWfStockSaveDto actWfStockSaveDto) {
        return toAjax(iActWfStockService.saveWfStock(actWfStockSaveDto));
    }

    /**
     * 撤销流程
     */
    @PreAuthorize("@ss.hasPermi('activiti:stock:edit')")
    @Log(title = "样本单据撤销 ", businessType = BusinessType.OTHER)
    @PostMapping(value = "/revoke")
    public AjaxResult revoke(@RequestBody String businessKey) {
        return toAjax(iActWfStockService.revoke(businessKey));
    }

    /**
     * 修改出入库单据
     */
    @PreAuthorize("@ss.hasPermi('activiti:stock:edit')")
    @Log(title = "样本单据 ", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ActWfStock actWfStock) {
        return toAjax(iActWfStockService.updateById(actWfStock));
    }

    /**
     * 删除出入库单据
     */
    @PreAuthorize("@ss.hasPermi('activiti:stock:remove')")
    @Log(title = "样本单据 ", businessType = BusinessType.DELETE)
    @DeleteMapping("/{stockIds}")
    public AjaxResult remove(@PathVariable String[] stockIds) {
        return toAjax(iActWfStockService.removeByIds(Arrays.asList(stockIds)));
    }
}
